<template>
    <div class="materialsAudit-container">
        <BaseAudit ref="baseAudit"
                   :beforeSubmitFn="beforeSubmitFn"
                   :callbackFn="callbackFn"
        >
            <template slot="extraOption" v-if="canChoosePerson">
                <title-name title="选择责任人"></title-name>
                <div class="mb20">
                    <a-row>
                        <a-col :span="12">
                            <a-form-model-item class="approval-item" label="责任人" prop="orgSelectedName">
                                <a-tag class="mb10p" :key="item.id" v-for="(item,index) of form1.personLiable">
                                    {{ item.name }}
                                </a-tag>
                                <a-tag style="background: #fff; borderStyle: dashed;" @click="selectPersonVisible = true">
                                    <a-icon type="plus" />
                                    选择人员
                                </a-tag>
                            </a-form-model-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-model-item class="approval-item" label="处理时限" prop="processingTime">
                                <a-date-picker v-model="form1.processingTime" valueFormat="x" />
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </div>
            </template>
        </BaseAudit>

        <SelectPerson
            title="选择责任人"
            mode="single"
            :visible.sync="selectPersonVisible"
            :list.sync="form1.personLiable"></SelectPerson>
    </div>
</template>

<script>
import BaseAudit from '@/views/matters/approval/components/BaseAudit'
import otherTypeApi from '@/api/legal/branchContract/otherType'
import TreeSelectOrganization from '@/components/TreeSelectOrganization'
import Jobs from '@/components/TreeSelectOrganization/jobs'
import SelectPerson from '@/components/Select/SelectPerson'
import riskListApi from '@/api/legal/risk/list'
import comments from '@/views/Legal/risk/list/components/comments'

export default {
    name: 'RiskAudit',
    components: {
        SelectPerson,
        BaseAudit,
        comments
    },
    data() {
        return {
            selectPersonVisible: false,
            form1: {
                id: '',
                personLiable: [],
                processingTime: null
            },
            taskDefKey: this.$route.query.taskDefKey,
            businessType: this.$route.query.businessType,
            businessId: this.$route.query.businessId,
            auditStatus: ''
        }
    },
    computed: {
        canChoosePerson() {
            return (
                this.taskDefKey === 'legal_risk_list_pm' ||
                this.taskDefKey === 'legal_risk_list_bce' ||
                this.taskDefKey === 'legal_risk_list_cld'
            ) && this.auditStatus === 'SUBMITTED'
        }
    },
    created() {
    },
    mounted() {
        this.getBaseInfo()
    },
    methods: {
        async getBaseInfo(request) {
            this.loading = true
            let res = await riskListApi.findByBusinessId({}, { id: this.businessId })
            if (res.code === this.SUCCESS_CODE) {
                this.form1.id = res.data.id
                this.auditStatus = res.data.auditStatus
            }
            this.loading = false
        },
        async beforeSubmitFn(row) {
            if (!row.agree) {
                return true
            }
            if (this.canChoosePerson) {
                let submitForm = this.$clone(this.form1)
                submitForm.personLiable = submitForm.personLiable[0].userId
                let res = await riskListApi.editByRiskApply(submitForm)
                return res.code === this.SUCCESS_CODE
            }
            return true
        },
        callbackFn(type) {
            if (type !== 'approval') return true
            if (this.canChoosePerson) {
                if (!this.form1.personLiable.length) {
                    this.$noticeWarning('请选择责任人')
                    return false
                }
                if (!this.form1.processingTime) {
                    this.$noticeWarning('请选择处理时限')
                    return false
                }
            }
            return true
        }
    }
}
</script>


<style lang="less" scoped>
.materialsAudit-container {
    /deep/ .approval-item {
        display: flex;
    }
}
</style>
